How do I prevent one function from repeating until another function's if condition is met and it run